package org.sgr.money.manager.frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Composite;

public class SwtMain {

	private Shell sShell = null;
	private ToolBar toolBar = null;
	private ScrolledComposite welcomeScrolledComposite = null;
	private Composite composite = null;

	/**
	 * This method initializes sShell
	 */
	@SuppressWarnings("unused")
	private void createSShell() {
		sShell = new Shell(SWT.SHELL_TRIM);
		sShell.setText("money manager");
		sShell.setBackground(new Color(Display.getCurrent(), 240, 240, 240));
		FormLayout formLayout = new FormLayout();
		formLayout.marginWidth=0;
		formLayout.marginHeight=0;
		sShell.setLayout(formLayout);
		createScrolledComposite();
		createToolBar();
		sShell.setSize(new Point(600, 400));
	}

	/**
	 * This method initializes toolBar
	 * 
	 */
	private void createToolBar() {
		toolBar = new ToolBar(sShell, SWT.NONE);
		toolBar.setBounds(new Rectangle(0, 0, 0, 0));
		ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
		// toolItem.setImage(new Image(Display.getCurrent(),
		// getClass().getResourceAsStream("/pp11.jpg")));
		toolItem.setText("欢迎");
		ToolItem toolItem1 = new ToolItem(toolBar, SWT.PUSH);
		toolItem1.setText("账户");
		ToolItem toolItem2 = new ToolItem(toolBar, SWT.PUSH);
		toolItem2.setText("记账");
		ToolItem toolItem3 = new ToolItem(toolBar, SWT.PUSH);
		toolItem3.setText("查询统计");
		ToolItem toolItem4 = new ToolItem(toolBar, SWT.PUSH);
		toolItem4.setText("基础资料");
		final ToolItem toolItemHelp = new ToolItem(toolBar, SWT.DROP_DOWN);
		toolItemHelp.setText("帮助");

		final Menu muhelp = createMuHelp();

		toolItemHelp.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				if (event.detail == SWT.ARROW) {
					Rectangle rect = toolItemHelp.getBounds();// 取得按钮边框
					Point pt = new Point(rect.x, rect.y + rect.height);
					pt = toolBar.toDisplay(pt);// 转换为屏幕相对位置
					muhelp.setLocation(pt.x, pt.y);// 设置菜单
					muhelp.setVisible(true);// 显示菜单
				}
			}
		});
	}

	private Menu createMuHelp() {
		final Menu muhelp = new Menu(sShell, SWT.POP_UP);
		MenuItem miWelcom = new MenuItem(muhelp, SWT.PUSH);
		miWelcom.setText("欢迎");
		MenuItem miWizard = new MenuItem(muhelp, SWT.PUSH);
		miWizard.setText("向导");
		return muhelp;
	}

	/**
	 * This method initializes scrolledComposite
	 * 
	 */
	private void createScrolledComposite() {
		welcomeScrolledComposite = new ScrolledComposite(sShell, SWT.BORDER);
		// GridLayout gridLayout = new GridLayout(); // 创建网格布局对象
		welcomeScrolledComposite.setLayout(null);
		welcomeScrolledComposite.setExpandVertical(true);
		welcomeScrolledComposite.setExpandHorizontal(true);
		// gridLayout.marginHeight = 0;
		// gridLayout.marginWidth = 0;
		// scrolledComposite.setLayout(gridLayout); // 设置面板布局方式为网格布局
		//
		FormData data = new FormData();
		data.top = new FormAttachment(0, 32); // Label与shell上边框相距1象素
		data.left = new FormAttachment(0, 0); // Label与shell左边框相距1象素
		data.bottom = new FormAttachment(100, -10);// Label在shell水平中线上方5象素
		data.right = new FormAttachment(100, -10); // Label在shell垂直中线左边5象素
		welcomeScrolledComposite.setLayoutData(data);
		createComposite();
	}

	/**
	 * This method initializes composite	
	 *
	 */
	private void createComposite() {
		composite = new Composite(welcomeScrolledComposite, SWT.NONE);
		composite.setLayout(new GridLayout());
	}

}
